package com;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;


public class TestWxSendApp {
    private static RestTemplate restTemplate=new RestTemplate();
    static String APP_ID="wxc1263fcd7d9dd0d4";
    static String APP_SECRET="378bef8c920f397c4291deb92e24a81a";
    static String GET_TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
    static String SEND_MSG_URL="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
    static String TEMPLATE_ID="BEhCt-wz8DUY-nM7H3LFc5zVpzjRNNKNHHn0psTpqyA";

    public static void main(String[] args) {
        //得到accessToken
        String realTokenUrl = String.format(GET_TOKEN_URL, APP_ID, APP_SECRET);
        System.out.println(realTokenUrl);
        //发请求
        ResponseEntity<String> entity = restTemplate.getForEntity(realTokenUrl, String.class);
        String json = entity.getBody();
        System.out.println(json);
        JSONObject jsonObject= JSON.parseObject(json);
        String access_token = jsonObject.get("access_token").toString();
        System.out.println(access_token);

        //组装要推送的数据
        WxOfficialModel model=new WxOfficialModel();
        model.setToUser("ot6Io6qDw4Vu96r0T6kVCWNy5EiA");
        model.setTemplateId(TEMPLATE_ID);
        model.setTopColor("#0000FF");
        model.setUrl("https://www.baidu.com/");
        Map<String, Map<String, String>> data=new HashMap<>();
        data.put("User",WxOfficialModel.build("张先生","#173177"));
        data.put("Date",WxOfficialModel.build("06月07日 19时24分","#173177"));
        data.put("CardNumber",WxOfficialModel.build("0426","#173177"));
        data.put("Type",WxOfficialModel.build("消费","#173177"));
        data.put("Money",WxOfficialModel.build("人民币260.00元","#173177"));
        data.put("DeadTime",WxOfficialModel.build("06月07日19时24分","#173177"));
        data.put("Left",WxOfficialModel.build("65040000.09","#173177"));
        model.setData(data);

        //推送
        String realSendUrl=String.format(SEND_MSG_URL,access_token);
        String post = restTemplate.postForObject(realSendUrl, model, String.class);
        System.out.println(post);


    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class WxOfficialModel {

    //{touser:"",template_id:""}
    @JsonProperty("touser")
    private String toUser;

    @JsonProperty("template_id")
    private String templateId;

    private String url;

//    @JsonProperty("topcolor")
    private String topColor;

    private Map<String, Map<String, String>> data;

    public static Map<String, String> build(String value, String color) {
        HashMap<String, String> map = new HashMap<>(4);
        map.put("value", value);
        map.put("color", color);
        return map;
    }
}
